import numpy as np

from cie_y_integral import compute_cie_y_integral, compute_inner_product_with_cie_y

cie_d65_interleaved = [
    300.000000, 0.034100, 305.000000, 1.664300, 310.000000, 3.294500, 315.000000,
    11.765200, 320.000000, 20.236000, 325.000000, 28.644699, 330.000000, 37.053501,
    335.000000, 38.501099, 340.000000, 39.948799, 345.000000, 42.430199, 350.000000,
    44.911701, 355.000000, 45.775002, 360.000000, 46.638302, 365.000000, 49.363701,
    370.000000, 52.089100, 375.000000, 51.032299, 380.000000, 49.975498, 385.000000,
    52.311798, 390.000000, 54.648201, 395.000000, 68.701500, 400.000000, 82.754898,
    405.000000, 87.120399, 410.000000, 91.486000, 415.000000, 92.458900, 420.000000,
    93.431801, 425.000000, 90.056999, 430.000000, 86.682297, 435.000000, 95.773598,
    440.000000, 104.864998, 445.000000, 110.935997, 450.000000, 117.008003, 455.000000,
    117.410004, 460.000000, 117.811996, 465.000000, 116.335999, 470.000000, 114.861000,
    475.000000, 115.391998, 480.000000, 115.922997, 485.000000, 112.366997, 490.000000,
    108.810997, 495.000000, 109.082001, 500.000000, 109.353996, 505.000000, 108.578003,
    510.000000, 107.802002, 515.000000, 106.295998, 520.000000, 104.790001, 525.000000,
    106.238998, 530.000000, 107.689003, 535.000000, 106.046997, 540.000000, 104.404999,
    545.000000, 104.224998, 550.000000, 104.045998, 555.000000, 102.023003, 560.000000,
    100.000000, 565.000000, 98.167099, 570.000000, 96.334198, 575.000000, 96.061096,
    580.000000, 95.788002, 585.000000, 92.236801, 590.000000, 88.685600, 595.000000,
    89.345901, 600.000000, 90.006203, 605.000000, 89.802597, 610.000000, 89.599098,
    615.000000, 88.648903, 620.000000, 87.698700, 625.000000, 85.493599, 630.000000,
    83.288597, 635.000000, 83.493896, 640.000000, 83.699203, 645.000000, 81.862999,
    650.000000, 80.026802, 655.000000, 80.120697, 660.000000, 80.214600, 665.000000,
    81.246201, 670.000000, 82.277802, 675.000000, 80.280998, 680.000000, 78.284203,
    685.000000, 74.002701, 690.000000, 69.721298, 695.000000, 70.665199, 700.000000,
    71.609100, 705.000000, 72.978996, 710.000000, 74.348999, 715.000000, 67.976501,
    720.000000, 61.604000, 725.000000, 65.744797, 730.000000, 69.885597, 735.000000,
    72.486298, 740.000000, 75.086998, 745.000000, 69.339798, 750.000000, 63.592701,
    755.000000, 55.005402, 760.000000, 46.418201, 765.000000, 56.611801, 770.000000,
    66.805397, 775.000000, 65.094101, 780.000000, 63.382801, 785.000000, 63.843399,
    790.000000, 64.304001, 795.000000, 61.877899, 800.000000, 59.451900, 805.000000,
    55.705399, 810.000000, 51.959000, 815.000000, 54.699799, 820.000000, 57.440601,
    825.000000, 58.876499, 830.000000, 60.312500]

if __name__ == "__main__":
    cie_y_integral = compute_cie_y_integral()
    lambdas = cie_d65_interleaved[::2]
    assert all(y - x == lambdas[1] - lambdas[0] for x, y in zip(lambdas[:-1], lambdas[1:]))
    values = np.array(cie_d65_interleaved[1::2])
    lambda_min = lambdas[0]
    lambda_max = lambdas[-1]
    visible_min = 360
    visible_max = 830
    n = len(lambdas) - 1


    def sample(l):
        t = (l - lambda_min) / (lambda_max - lambda_min) * n
        if t < 0 or t > n:
            return 0.0
        i = np.clip(int(t), 0, n - 1)
        w = t - i
        print(l, t, i, w, lambdas[i])
        return (1.0 - w) * values[i] + w * values[i + 1]


    dense_samples = np.array([sample(l) for l in range(visible_min, visible_max + 1)])
    inner_product = compute_inner_product_with_cie_y(dense_samples)
    dense_samples *= cie_y_integral / inner_product
    for i in range(len(dense_samples)):
        print(f"{dense_samples[i]}f", end=", " if (i + 1) % 5 != 0 else ",\n")
